/*
 * convert.c
 *
 *  Created on: Jul 24, 2023
 *      Author: 42577
 */

#include "convert.h"
#include "math.h"

// f = k / (H ^2) + b
// H0 = 1.8, f1 = h0 * k + b
// H1 = 5.8, f2 = h1 * k + b
// k = (f2 - f1)/(h2 - h1)
// b = f1 - h0 * k
// h = (k / (f - b)) ^ 0.5

float key_bot_mm, key_top_mm;

void key_mm_init(void)
{
    key_bot_mm = 0.001f*KEY_BOT_UM;
    key_top_mm = 0.001f*KEY_TOP_UM;
}

void topbottom2kv(uint16_t top_value, uint16_t bottom_value, float* v0, float* k)
{
    *k = (bottom_value-top_value)*3.3f/4095/(1/key_bot_mm-1/key_top_mm);
    *v0 = bottom_value*3.3f/4095-(*k)/key_bot_mm;
}

void rescale_kv(float* v0, float* k, int* key_v0, int* key_k)
{
    *key_k = 1000*1000/220*273*(*k);
    *key_v0 = 1000*273/220*(*v0);
}

